package com.fzy.modules.goods.web;

import com.fzy.core.common.ApiController;
import com.fzy.core.common.ResponseData;
import com.fzy.modules.goods.entity.GoodsBrand;
import com.fzy.modules.goods.service.GoodsBrandService;
import com.fzy.modules.goods.service.GoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *商品品牌Controller
 */
@RestController
@RequestMapping(value = "/api/goods/brand")
public class GoodsBrandController extends ApiController{
   @Autowired
    GoodsBrandService goodsBrandService;
   @Autowired
    GoodsService goodsService;

    @GetMapping(value = "/findAll")
    public List<GoodsBrand> findAll(){
        return goodsBrandService.findAll();
    }
    @PostMapping(value = "")
    public ResponseEntity<ResponseData> save(GoodsBrand goodsBrand){
        if(StringUtils.isNotEmpty(goodsBrand.getId())){
            return new ResponseEntity<>(success(goodsBrandService.update(goodsBrand)),HttpStatus.OK);
        }

        return new ResponseEntity<>(success(goodsBrandService.save(goodsBrand)),HttpStatus.OK);
    }
    @DeleteMapping(value = "/{id}")
    public ResponseEntity<ResponseData> delete(@PathVariable String id) {
        if(goodsBrandService.isChildrenBrand(id)>0){
            return new ResponseEntity<>(fail("该品牌有子级品牌，不能删除。"),HttpStatus.BAD_GATEWAY);
        }
       if(goodsService.isWithGoodsUnderBrand(id)>0){
           return new ResponseEntity<>(fail("该品牌属于某产品，不能删除。"),HttpStatus.BAD_GATEWAY);
       }
        return new ResponseEntity<>(success(goodsBrandService.delete(id)), HttpStatus.OK);
    }

}
